package com.szxy.frontend.portal.service.impl;

import com.szxy.frontend.portal.feign.CommonItemsFeignClient;
import com.szxy.frontend.portal.feign.CommonRedisFeiginClient;
import com.szxy.frontend.portal.service.ItemCategoryService;
import com.szxy.pojo.TbItemCat;
import com.szxy.pojo.TbItemDesc;
import com.szxy.utils.CatResult;
import com.szxy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:zwer
 * @Date:2019/9/10 20:33
 * @Description:com.szxy.frontend.portal.service
 * @Version:1.0
 **/
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Autowired
    private CommonItemsFeignClient commonItemsFeignClient;
    @Autowired
    private CommonRedisFeiginClient commonRedisFeiginClient;

    @Override
    public Result selectItemCategoryAll() {
        //先查询 Redis 缓存中是否存在 CatResult 缓存
        try {
            CatResult catResult = this.commonRedisFeiginClient.getItemCatResult();
            if (catResult != null && catResult.getData() != null && catResult.getData().size() > 0) {
                return Result.ok(catResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //若 Redis 缓存未命中，则查询关系型数据库
        CatResult catResult = this.commonItemsFeignClient.selectItemCategoryAll();
        try {
            if (catResult != null && catResult.getData() != null && catResult.getData().size() > 0) {
                //将 CatResult 放入 Redis 缓存中
                this.commonRedisFeiginClient.insertItemCatResult(catResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (catResult != null && catResult.getData() != null && catResult.getData().size() > 0) {
            return Result.ok(catResult);
        }
        return Result.error("查询错误");
    }


}
